<template>
  <div class="more-menu" v-clickoutside="()=>{showMenu = false}">
    <div class="menu" v-show="showMenu">
      <div class="menu-item">修改用户资料</div>
      <div class="menu-item">修改密码</div>
      <div class="menu-item" @click="logout">退出登录</div>
    </div>
    <div @click="showMenu = !showMenu">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import { api_logout } from '@/api/user'

export default {
  name: 'more-menu',
  data () {
    return {
      showMenu: false,
    }
  },
  methods: {
    // 点击退出登录的回调
    async logout () {
      let res = await api_logout()
      if (res.data.code === 200) {
        this.$router.push('/login')
        this.$message.info('退出登录成功!')
        // 断开websocket
        window.$socket.disconnect()
        window.$socket = null;
      }
    },
  },
}
</script>

<style scoped>
.more-menu {
  position: relative;
}

.menu {
  position: absolute;
  background-color: #fff;
  width: 120px;
  font-size: 12px;
  padding: 3px 0;
  left: 40px;
  bottom: 8px;
  border-radius: 5px;
  z-index: 1;
}

.menu-item {
  border-bottom: 1px solid #ccc;
  padding: 4px 8px;
  cursor: pointer;
}

.menu-item:hover {
  background-color: #129611;
  color: white;
}

.menu-item:last-child {
  border: none;
}
</style>